package com.kedll.library.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kedll.library.R;
import com.kedll.library.util.ViewUtil;


/**
 *
 * @author sendor
 * @date 16/5/30
 */
public class AccountItemView extends LinearLayout {

    private final LinearLayout mContentLayout;
    private final LinearLayout mNextIconLayout;
    ImageView mIconImage;
    TextView mTitle;
    TextView mValue;
    TextView mSubTitle;
    TextView mEditSubTitle;
    ImageView mNextIcon;
    View mWidgetFrame;
    private boolean enable;

    public TextView getTitleView() {
        return mTitle;
    }

    public TextView getValueView() {
        return mValue;
    }

    public TextView getEditSubTitle() {
        return mEditSubTitle;
    }

    public TextView getSubTitleView() {
        return mSubTitle;
    }

    public ImageView getIconImage() {
        return mIconImage;
    }

    public ImageView getNextIcon() {
        return mNextIcon;
    }

    public AccountItemView(Context context) {
        this(context, null);
    }

    /**
     * @param context
     * @param attrs
     */
    public AccountItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.account_item, this, true);
        mTitle = findViewById(R.id.title);
        mValue = findViewById(R.id.value);
        mSubTitle = findViewById(R.id.subtitle);
        mEditSubTitle = findViewById(R.id.edit_subtitle);
        mIconImage = findViewById(R.id.icon_image);
        mNextIcon = findViewById(R.id.nextIcon);
        mWidgetFrame = findViewById(android.R.id.widget_frame);
        mContentLayout = findViewById(R.id.content_layout);
        mNextIconLayout = findViewById(R.id.nextIcon_layout);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AccountItemView);
        Drawable icon = a.getDrawable(R.styleable.AccountItemView_icon_image);
        Drawable nextIcon = a.getDrawable(R.styleable.AccountItemView_next_icon);
        ColorStateList titleColor = a.getColorStateList(R.styleable.AccountItemView_title_color);
        ColorStateList subTitleColor = a.getColorStateList(R.styleable.AccountItemView_subtitle_color);
        ColorStateList valueColor = a.getColorStateList(R.styleable.AccountItemView_value_color);
        boolean isShowNextIcon = a.getBoolean(R.styleable.AccountItemView_show_next_icon, true);
        String title = a.getString(R.styleable.AccountItemView_acTitle);
        String value = a.getString(R.styleable.AccountItemView_value);
        String subtitle = a.getString(R.styleable.AccountItemView_acSubTitle);
        int inputType = a.getInt(R.styleable.AccountItemView_android_inputType, EditorInfo.TYPE_NULL);
        boolean editable = a.getBoolean(R.styleable.AccountItemView_android_editable, false);
        String hint = a.getString(R.styleable.AccountItemView_android_hint);
        int hintColor = a.getColor(R.styleable.AccountItemView_subTitle_hint_color, ContextCompat.getColor(getContext(), android.R.color.darker_gray));
        enable = a.getBoolean(R.styleable.AccountItemView_enable, true);
        int acTitleSize = a.getInt(R.styleable.AccountItemView_acTitle_size, 15);
        int subTitleSize = a.getInt(R.styleable.AccountItemView_subTitle_size, 15);
        int valueTitleSize = a.getInt(R.styleable.AccountItemView_value_size, 15);
        int gravity = a.getInt(R.styleable.AccountItemView_android_gravity, Gravity.CENTER_VERTICAL);
        int subTitleMarginLeft = a.getDimensionPixelOffset(R.styleable.AccountItemView_subTitle_marginLeft, -1);
        int titleMarginLeft = a.getDimensionPixelOffset(R.styleable.AccountItemView_title_marginLeft, -1);
        int titleEms = a.getInt(R.styleable.AccountItemView_android_ems, -1);
        int subTitleGravity = a.getInt(R.styleable.AccountItemView_subTitle_gravity, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        Drawable subTitleBackground = a.getDrawable(R.styleable.AccountItemView_subTitle_background);
        a.recycle();

        if (icon != null) {
            mIconImage.setImageDrawable(icon);
        } else {
            ViewUtil.setGone(mIconImage, true);
        }

        mEditSubTitle.setInputType(inputType);
        setEditable(editable);

        mEditSubTitle.setGravity(subTitleGravity);
        mSubTitle.setGravity(subTitleGravity);

        if (titleEms != -1) {
            mTitle.setEms(titleEms);
        }

        if (subTitleMarginLeft != -1) {
            MarginLayoutParams params = (MarginLayoutParams) mSubTitle.getLayoutParams();
            params.leftMargin = subTitleMarginLeft;
        }
        if (titleMarginLeft != -1) {
            MarginLayoutParams params = (MarginLayoutParams) mTitle.getLayoutParams();
            params.leftMargin = titleMarginLeft;
        }

        this.setEnable(enable);

        if (!TextUtils.isEmpty(hint)) {
            mEditSubTitle.setHint(hint);
            mSubTitle.setHint(hint);
        }

        mEditSubTitle.setHintTextColor(hintColor);
        mSubTitle.setHintTextColor(hintColor);


        if (titleColor != null) {
            mTitle.setTextColor(titleColor);
        }

        if (subTitleColor != null) {
            mSubTitle.setTextColor(subTitleColor);
            mEditSubTitle.setTextColor(subTitleColor);
        }

        if (subTitleBackground != null) {
            mSubTitle.setBackground(subTitleBackground);
        }


        if (valueColor != null) {
            mValue.setTextColor(valueColor);
        }

        if (nextIcon != null) {
            mNextIcon.setImageDrawable(nextIcon);
        }


        setShow_next_icon(isShowNextIcon);

        mTitle.setText(title);
        mValue.setText(value);
        mSubTitle.setText(subtitle);
        mEditSubTitle.setText(subtitle);
        mTitle.setTextSize(acTitleSize);
        mSubTitle.setTextSize(subTitleSize);
        mEditSubTitle.setTextSize(subTitleSize);
        mValue.setTextSize(valueTitleSize);
        mContentLayout.setGravity(gravity);
        mNextIconLayout.setGravity(gravity);
        mSubTitle.setInputType(inputType);
    }

    public void setShow_next_icon(boolean isShow) {
        ViewUtil.setGone(mNextIconLayout, !isShow);

    }

    public void setEnable(boolean enable) {
        this.enable = enable;
        if (!enable) {
            mWidgetFrame.setBackgroundResource(R.color.white);
        } else {
            mWidgetFrame.setBackgroundResource(R.drawable.drawable_setting_selector);
        }
    }

    public void setIcon(int iconId) {
        if (mIconImage != null) {
            mIconImage.setImageResource(iconId);
        }
    }

    public void setNextIcon(int iconId) {
        if (mNextIcon != null) {
            if (iconId == -1) {
                mNextIcon.setImageDrawable(null);
            } else {
                mNextIcon.setImageResource(iconId);
            }
        }
    }

    public void setAcTitle(CharSequence title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    public void setAcSubTitle(CharSequence title) {
        if (mSubTitle != null) {
            mSubTitle.setText(title);
            mEditSubTitle.setText(title);
        }
    }

    public void setValue(CharSequence value) {
        if (mValue != null) {
            mValue.setText(value);
        }
    }

    public String getValue() {
        if (mValue != null) {
            return mValue.getText().toString();
        }

        return "";
    }

    public String getAcSubTitle() {
        if (mSubTitle != null && mSubTitle.getVisibility() == VISIBLE) {
            return mSubTitle.getText().toString();
        } else if (mEditSubTitle != null && mEditSubTitle.getVisibility() == VISIBLE) {
            return mEditSubTitle.getText().toString();
        }
        return "";
    }

    public String getEditSutTitleValue() {
        if (mEditSubTitle != null) {
            return mEditSubTitle.getText().toString();
        }
        return "";
    }

    public void setEditable(boolean editable) {
        ViewUtil.setGone(mEditSubTitle, !editable);
        ViewUtil.setGone(mSubTitle, editable);
    }

    public void setTitleColor(int color) {
        mTitle.setTextColor(color);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(l);
    }
}
